package com.tosit.bmx.controller.admin;

import com.tosit.bmx.common.json.JSONModel;
import com.tosit.bmx.common.json.JSONUtil;
import com.tosit.bmx.common.util.DateUtil;
import com.tosit.bmx.common.util.Sha1Util;
import com.tosit.bmx.domian.Area;
import com.tosit.bmx.domian.AreaUser;
import com.tosit.bmx.domian.User;
import com.tosit.bmx.service.admin.AreaService;
import com.tosit.bmx.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 管理员操作用户的类
 *
 * @author tsl
 * @create 2018-04-23 10:39
 **/
@Controller
@RequestMapping()
public class AUserController {
    @Autowired
    UserService userService;

    @Autowired
    AreaService areaService;

    /**
     * 添加用户，并为此用户关联权限(题型没被禁用的)
     *
     * @param user 用户对象
     * @return
     * @author tsl
     * @date 2018/4/23 10:41
     */
    @RequestMapping(value = "/user", method = RequestMethod.POST)
    public void addUser(User user, Integer sareaId, Integer careaId, Integer jareaId, HttpServletResponse response) {
        User userByName = userService.findsUserByName(user.getTel());
        if (userByName == null) {
            user.setCreatetime(new Date().getTime()/1000 + "");
            user.setTimelimit(DateUtil.parseStrToDate(user.getTimelimit(), "yyyy-MM-dd").getTime()/1000+ "");
            user.setPassword(Sha1Util.getSha1("123456"));
            Integer result = addUserAndrelateAreaLevel(user, sareaId, careaId, jareaId);
            if (result != null) {
                JSONModel.put("status", "success");
            } else {
                JSONModel.put("status", "fail");
            }
        } else {
            JSONModel.put("msg", "添加失败，用户账号名已存在,请重新添加");
        }
        JSONUtil.printJsonModel(response);
    }
    @Transactional
    public Integer addUserAndrelateAreaLevel(User user, Integer sareaId, Integer careaId, Integer jareaId) {
        Integer result = userService.adduser(user);
        Integer v = userService.relateLevel(user.getId());
        if (sareaId != null && careaId != null && jareaId != null) {
            List<AreaUser> list = new ArrayList<>();
            AreaUser areaUser1 = new AreaUser();
            areaUser1.setAid(sareaId);
            areaUser1.setUid(user.getId());
            AreaUser areaUser2 = new AreaUser();
            areaUser2.setAid(careaId);
            areaUser2.setUid(user.getId());
            AreaUser areaUser3 = new AreaUser();
            areaUser3.setAid(jareaId);
            areaUser3.setUid(user.getId());
            list.add(areaUser1);
            list.add(areaUser2);
            list.add(areaUser3);
            areaService.relateUser(list);
        }
        return result;
    }

    /**
     * 获取某个区域的用户
     *
     * @param id   区域id
     * @param type 民族  1汉人 2藏
     * @return
     * @author tsl
     * @date 2018/4/23 10:41
     */
    @RequestMapping(value = "/userByArea", method = RequestMethod.GET)
    public void gainUserByArea(Integer id, Integer type, HttpServletResponse response) {
        List<User> result = userService.gainUserByArea(id, type);
        if (result != null) {
            JSONModel.put("data", result);
        } else {
            JSONModel.put("status", "fail");
        }
        JSONUtil.printJsonModel(response);
    }

    /**
     * 获取未加入区域的用户
     *
     * @param type 民族  1汉人 2藏
     * @return
     * @author tsl
     * @date 2018/4/23 10:41
     */
    @RequestMapping(value = "/NAreaUser", method = RequestMethod.GET)
    public void gainNAreaUser(Integer type, HttpServletResponse response) {
        List<User> result = userService.gainNAreaUser(type);
        if (result != null) {
            JSONModel.put("data", result);
        } else {
            JSONModel.put("status", "fail");
        }
        JSONUtil.printJsonModel(response);
    }

    /**
     * 修改某一用户(禁用和启用也可以在这处理)
     *
     * @param user
     * @return
     * @author tsl
     * @date 2018/4/23 13:24
     */
    @RequestMapping(value = "/user", method = RequestMethod.PUT)
    public void modifyUser(User user, HttpServletResponse response) {
        Integer result = userService.modifyUser(user);
        if (result != null) {
            JSONModel.put("status", "success");
        } else {
            JSONModel.put("status", "fail");
        }
        JSONUtil.printJsonModel(response);
    }

    /**
     * 删除一个或多个用户
     * js调用具体使用可以参考web下 Test.jsp
     *
     * @param ids      删除的所有用户id
     * @param response
     * @author tsl
     * @date 2018/4/22 16:17
     */
    @RequestMapping(value = "/user", method = RequestMethod.DELETE)
    public void removeUser(@RequestBody List<Integer> ids, HttpServletResponse response) {
        Integer result = userService.removeUser(ids);
        if (result != null) {
            JSONModel.put("status", "success");
        } else {
            JSONModel.put("status", "fail");
        }
        JSONUtil.printJsonModel(response);
    }

    /**
     * 根据区域和用户电话，用户名搜索某一用户
     *
     * @param searchCode 搜所关键词
     * @param type       民族  1汉人 2藏
     * @param areaId  当id为0未分组
     * @author tsl
     * @date 2018/4/23 13:24
     */
    @RequestMapping(value = "/seekUser", method = RequestMethod.GET)
    public void seekUser(Integer areaId, String searchCode, Integer type, HttpServletResponse response) {
        List<User> result = null;
        /*if (areaId == 0) {
            result = userService.seekUser(searchCode, type);
        } else {
            result = userService.seekUser(areaId, searchCode, type);
        }*/

        result = userService.seekUser(searchCode, type);
        if (result != null) {
            JSONModel.put("data", result);
        } else {
            JSONModel.put("status", "fail");
        }
        JSONUtil.printJsonModel(response);
    }

    /**
     * 根据区域和用户状态搜索用户（1正常使用  2到期用户 3停用用户 注：全部用户可使用在上/userByArea接口）
     *
     * @param areaId 区域id  当id为0未分组
     * @param status 查询类型  1正常使用  2到期用户 3停用用户
     * @param type   民族  1汉人 2藏
     * @author tsl
     * @date 2018/4/23 13:24
     */
    @RequestMapping(value = "/seekUser/{areaId},{status},{type}", method = RequestMethod.GET)
    public void seekUser(@PathVariable Integer areaId, @PathVariable Integer type, @PathVariable Integer status, HttpServletResponse response) {
        List<User> result = null;
        if (areaId == 0) {
            result = userService.seekUser(type, status);
        } else {
            result = userService.seekUser(areaId, type, status);
        }
        if (result != null) {
            JSONModel.put("data", result);
        } else {
            JSONModel.put("status", "fail");
        }
        JSONUtil.printJsonModel(response);
    }

    /**
     * 重置某个用户密码
     *
     * @param userId 用户id
     * @return
     * @author tsl
     * @date 2018/4/24 15:34
     */
    @RequestMapping(value = "/user/reset", method = RequestMethod.PUT)
    public void resetPassWord(Integer userId, HttpServletResponse response) {
        Integer result = userService.resetPassWord(userId);
        if (result != null) {
            JSONModel.put("status", "success");
        } else {
            JSONModel.put("status", "fail");
        }
        JSONUtil.printJsonModel(response);
    }

    /**
     * 首页接口（获取在线用户，题目数量，用户数量）
     *
     * @return
     * @author tsl
     * @date 2018/4/24 15:34
     */
    @RequestMapping(value = "/user/online", method = RequestMethod.PUT)
    public void gainOnlineUser(HttpServletResponse response) {
        List<User> result = userService.gainOnlineUser();
        Integer userCount = userService.getUserCount();
        Integer subjectCount = userService.getSubjectCount();
        JSONModel.put("onlineUser", result);
        JSONModel.put("subjectCount", subjectCount);
        JSONModel.put("userCount", userCount);
        if (result != null) {
            JSONUtil.printByJSON(response, JSONModel.put());
        } else {
            JSONUtil.printByJSON(response, JSONModel.put("status", "fail"));
        }
    }



}
